﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Dao.Plant;
using SMTC.Model.Domain;
using SMTC.Model.Model;
using SMTC.BO.Common;
using System.Data.Common;

namespace SMTC.BO.Plant
{
    public class PlantProccessServices
    {
        private PlantProccessDao _dao = new PlantProccessDao();
        /// <summary>
        /// 插入工厂
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool InsertPlant(PlantDomain domain)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                domain = this.ProccessCapacity(domain);
                operResult = operResult && _dao.InsertPlant(domain.plant, ref dbt);
                foreach (var item in domain.buList)
                {
                    operResult = operResult && _dao.InsertPlantsBU(item, ref dbt);
                }

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 删除工厂
        /// </summary>
        /// <param name="LineID"></param>
        /// <returns></returns>
        public bool DeletePlant(string PlantID)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                operResult = operResult && _dao.DeletePlant(PlantID, ref dbt);
                operResult = operResult && _dao.DeletePlantsBU(PlantID, ref dbt);

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 更新工厂信息
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool UpdatePlant(PlantDomain domain)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                domain = this.ProccessCapacity(domain);
                operResult = operResult && _dao.UpdatePlant(domain.plant, ref dbt);
                operResult = operResult && _dao.DeletePlantsBU(domain.plant.PlantID, ref dbt);
                foreach (var item in domain.buList)
                {
                    operResult = operResult && _dao.InsertPlantsBU(item, ref dbt);
                }

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }

        /// <summary>
        /// 计算生产线能力
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public PlantDomain ProccessCapacity(PlantDomain domain)
        {
            var result = from item in domain.buList
                         group item by item.PlantID into g
                         select new SMT_BUModel
                         {
                             BUCapacity = g.Sum(a => a.BUCapacity)
                         };
            List<SMT_BUModel> list = result.Cast<SMT_BUModel>().ToList<SMT_BUModel>();
            if (list != null && list.Count > 0)
            {
                domain.plant.PlantCapacity = list[0].BUCapacity;
            }
            return domain;
        }
    }
}
